package com.ykq.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.ykq.Product;
import com.ykq.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

//@RestController
//@RequestMapping("order")
//@Slf4j
public class OrderController3 {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("createOrder/{pid}")
    @SentinelResource(fallback = "fallbackMethod",blockHandler = "blockHandlerMethod",value = "xiadan")
    //sentinel把熔断进行细分化。 fallback:处理异常的。 要和该方法的参数一致，参数的最后加入Throwable 返回类型一致。
    // blockhandler:处理熔断的。
    public String createOrder(@PathVariable("pid")Integer pid){
        Product product = restTemplate.getForObject("http://shop-product-sentinel/product/findById/" + pid, Product.class);
        return JSON.toJSONString(product);
    }
    public String fallbackMethod(Integer pid,Throwable e){
        return "调用了异常处理方法";
    }

    public String blockHandlerMethod(Integer pid, BlockException exceptionHandler){
        return "调用了熔断降级的方法";
    }

}
